package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController{

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value="/list" ,name="进入角色管理页面")
    public String findAll(@RequestParam(name="page",defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Role> pageInfo = roleService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo);
        return "system/role/role-list";
    }

    @RequestMapping(value="/toAdd" ,name="进入角色新增页面")
    public String toAdd(){
 
        return "system/role/role-add";
    }

    @RequestMapping(value="/edit" ,name="保存角色方法")
    public String edit(Role role){

//        role的id如果为空就是新增，反之就是修改
        if(StringUtils.isEmpty(role.getId())){ //id为空
            //        新增时需要赋id
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            role.setCreateTime(new Date());
            role.setId(UUID.randomUUID().toString());  //id 赋值成一个随机id
            roleService.save(role);
        }else {  //id不为空
            role.setUpdateTime(new Date());
            roleService.update(role);
        }

        return "redirect:/system/role/list.do";  //重定向到列表数据
    }
    @RequestMapping(value="/toUpdate" ,name="进入修改角色页面")
    public String toUpdate(String id){
//         根据id查询角色
        Role role = roleService.findById(id);
//        把role放入request域中
        request.setAttribute("role",role);
//        修改页面和新增页面公用一套
        return "system/role/role-add";
    }
    @RequestMapping(value="/delete" ,name="删除角色方法")
    public String delete(String id){
        roleService.deleteById(id);
        return "redirect:/system/role/list.do";  //重定向到列表数据
    }
    @RequestMapping(value="/roleModule" ,name="进入到给角色分配权限的页面")
    public String roleModule(String roleid){ //
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module";
    }
    @RequestMapping(value="/getZtreeNodes" ,name="获取分配权限需要的树形节点数据")
    @ResponseBody // 1、转json 2、直接返回到浏览器上
    public List<Map> getZtreeNodes(String roleid){ //
//        查询当前角色拥有的权限ids
        List<String> moduleIdList = moduleService.findModulsIdsByRoleId(roleid);

//        [{id:1,name:"名称",pId:0}]
        List<Module> moduleList = moduleService.findAll();
//        重新组装节点数据
        List<Map> list = new ArrayList<>();
        Map map = null;
//  当前角色拥有的权限ids      "ad3be6e7-551f-45b6-b816-b2b751e204ed,9c5eb9b0-54a4-48bb-aab4-0d1d46cdfbbd,2,202"
        for (Module module : moduleList) {
            map = new HashMap();
            map.put("id",module.getId());
            map.put("name",module.getName());
            map.put("pId",module.getParentId());
            if(moduleIdList.contains(module.getId())){
                map.put("checked",true);
            }

            list.add(map);
        }
        return list;
    }

    @RequestMapping(value="/updateRoleModule" ,name="给角色分配菜单权限")
    public String updateRoleModule(String roleid,String moduleIds){ //  moduleIds "1212,3434,56565"
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";  //重定向到列表数据
    }

}